﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZSZ.DTO;
using ZSZ.Iservice;
using ZSZ.Service.Entity;

namespace ZSZ.Service
{
    public class RoleService : IRoleService
    {
        public long AddNew(string roleName)
        {
            using (ZSZDBcontext ct = new ZSZDBcontext())
            {
                CommonService<RoleEntity> cs = new CommonService<RoleEntity>(ct);
                bool exist = cs.GetAll().Any(r => r.Name == roleName);
                if (exist)
                {
                    throw new ArgumentException("角色名字已经存在");
                }
                RoleEntity role = new RoleEntity();
                role.Name = roleName;
                ct.Roles.Add(role);
                ct.SaveChanges();
                return role.Id;
            }
        }

        public void AddRoles(long adminUserId, long[] roleIds)
        {
            using (ZSZDBcontext ct = new ZSZDBcontext())
            {
                CommonService<AdminUserEntity> cs = new CommonService<AdminUserEntity>(ct);
                var adminUser = cs.GetById(adminUserId);
                if (adminUser == null)
                {
                    throw new ArgumentException("没有这个adminUser");
                }
                var roles = ct.Roles.Where(r => r.IsDeleted == false && roleIds.Contains(r.Id)).ToList();
                foreach (var item in roles)
                {
                    adminUser.Roles.Add(item);
                }
                ct.SaveChanges();
            }
        }
        private RoleDTO ToDTO(RoleEntity e)
        {
            RoleDTO dto = new RoleDTO();
            dto.CreateTime = e.CreateDatatime;
            dto.Id = e.Id;
            dto.Name = e.Name;
            return dto;
        }
        public RoleDTO[] GetAll()
        {
            using (ZSZDBcontext ct = new ZSZDBcontext())
            {
                CommonService<RoleEntity> cs = new CommonService<RoleEntity>(ct);
                return cs.GetAll().ToList().Select(r => ToDTO(r)).ToArray();
            }
        }

        public RoleDTO[] GetByAdminUserId(long adminUserId)
        {
            using (ZSZDBcontext ct = new ZSZDBcontext())
            {
                CommonService<AdminUserEntity> cs = new CommonService<AdminUserEntity>(ct);
                var adminUser = cs.GetById(adminUserId);
                if (adminUser == null)
                {
                    throw new ArgumentException("没有这个adminUser");
                }
                return adminUser.Roles.Select(r => ToDTO(r)).ToArray();
            }
        }

        public RoleDTO GetById(long id)
        {
            using (ZSZDBcontext ct = new ZSZDBcontext())
            {
                CommonService<RoleEntity> cs = new CommonService<RoleEntity>(ct);
                var role = cs.GetById(id);
                if (role == null)
                {
                    throw new ArgumentException("没有这个role");
                }
                return ToDTO(role);
            }
        }

        public RoleDTO GetByName(string name)
        {
            using (ZSZDBcontext ct = new ZSZDBcontext())
            {
                CommonService<RoleEntity> cs = new CommonService<RoleEntity>(ct);
                var role = cs.GetAll().SingleOrDefault(r => r.Name == name);
                if (role == null)
                {
                    throw new ArgumentException("没有这个role");
                }
                return ToDTO(role);
            }
        }

        public void MarkDeleted(long roleId)
        {
            using (ZSZDBcontext ct = new ZSZDBcontext())
            {
                CommonService<RoleEntity> cs = new CommonService<RoleEntity>(ct);
                cs.MarkDeleted(roleId);
            }
        }

        public void Update(long roleId, string roleName)
        {
            using (ZSZDBcontext ct = new ZSZDBcontext())
            {
                CommonService<RoleEntity> cs = new CommonService<RoleEntity>(ct);
                var role = cs.GetById(roleId);
                if (role == null)
                {
                    throw new ArgumentException("没有这个role");
                }
                role.Name = roleName;
                ct.SaveChanges();
            }
        }

        public void UpdateRoleIds(long adminUserId, long[] rolesId)
        {
            using (ZSZDBcontext ct = new ZSZDBcontext())
            {
                CommonService<AdminUserEntity> cs = new CommonService<AdminUserEntity>(ct);
                var adminUser = cs.GetById(adminUserId);
                if (adminUser == null)
                {
                    throw new ArgumentException("没有这个adminUser");
                }
                adminUser.Roles.Clear();
                var roles = ct.Roles.Where(r => r.IsDeleted == false && rolesId.Contains(r.Id));
                foreach (var item in roles)
                {
                    adminUser.Roles.Add(item);
                }
                ct.SaveChanges();
            }
        }
    }
}
